<?php

namespace App\Admin\Controllers;

use App\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use App\ParamType;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;

class ParamTypeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '参数分组';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ParamType());
        $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            $create->text('name', '分组名称');
        });
        $grid->column('id', __('Param Type Id'));
        $grid->column('name', __('Param Type Name'))->display(function ($name) {
            return '<a href="' . admin_url('param-values') . '?type=' . $this->id . '">' . $name . '</a>';
        }); //->expand(function ($model) {
//            $params = $model->params->take(10)->map(function ($param) {
//                return $param->only(['id', 'name', 'created_at']);
//            });
//            $table = new Table(['参数 ID', '参数名称', '发布时间'], $params->toArray());
//            $form = new \Encore\Admin\Widgets\Form();
//            $form->action(admin_url('param-values'));
//            $form->text('name', __('参数名称'))->required();
//            $form->hidden('type_id')->default($model->id);
//            $form->disableReset();
//            return new Box('', $form, $table);
        // });
        $grid->column('count', __('Param Type Count'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ParamType::findOrFail($id));

        $show->field('id', __('Param Type Id'));
        $show->field('name', __('Param Type Name'));
        $show->field('count', __('Param Type Count'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ParamType());
        $form->setAction('/admin/param-types');
        $form->text('name', __('Param Type Name'));
        return $form;
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
//            ->row(function (Row $row) {
//                $row->column(12, function (Column $column) {
//                    $form = new \Encore\Admin\Widgets\Form();
//                    $form->action(admin_url('param-types'));
//                    $form->text('name', __('分组名称'))->required();
//                    $form->hidden('_token')->default(csrf_token());
//                    $column->append((new Box(__('新建参数分组'), $form))->style('success'));
//                });
//
//            })
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form());
    }
}
